package cc.linker.steplink.crm.commons.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TransactionOrderUtil {

    private static long systemTimeMillis = 0L;
    private static long synchronizedNum = 0L;
    private static StringBuilder primaryUniqueKey;

    // 打赏订单：A；
    // 内容付费订单：B；
    // 红包订单：C；
    // 商品订单：E；
    // 充值订单：R；
    // 提现订单：F
    // 国学娃娃报名活动订单 ：Y
    // vip开通 ：V
    // 积分订单：I
    public static String TYPE_REWARD = "A";
    public static String TYPE_PAYCONTENT = "B";
    public static String TYPE_REDBAG = "C";
    public static String TYPE_SHOP = "E";
    public static String TYPE_REFUND = "G";
    public static String TYPE_RECHARGE = "R";
    public static String TYPE_WITHDRAW = "F";
    public static String TYPE_ENROLL = "Y";
    public static String TYPE_VIP = "V";
    public static String TYPE_INTEGRAL = "I";

    //业务单号编号
    //A: 商品成交
    public static String BUSI_TYPE_SHOPDEAL = "A";
    //B: 联盟商家开通
    public static String BUSI_TYPE_XBOPEN = "B";
    //C: 加盟代理开通
    public static String BUSI_TYPE_AGENTOPEN = "C";
    //D: 平台抽成
    public static String BUSI_TYPE_PLATFORM = "D";
    //E: 提现
    public static String BUSI_TYPE_WITHDRAW = "E";
    //F: 提现失败
    public static String BUSI_TYPE_WITHDRAWFAIL = "F";
    //G: 反润-服务费
    public static String BUSI_TYPE_SERVICEFEE = "G";
    //H: 反润-推荐费
    public static String BUSI_TYPE_RECOMMENDDFEE = "H";
    //I: 体检费支付
    public static String BUSI_TYPE_EXAMFEE = "I";
    //J：体检名额充值
    public static String BUSI_TYPE_EXAM_RECHARGE = "J";


    /**
     * 获得一个数据记录的主键Key
     *
     * @return 全局唯一主键
     */
    public synchronized static String getPrimaryKey() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        primaryUniqueKey = new StringBuilder(25);
        long currentTimeMillis = System.currentTimeMillis();
        String key;
        //同步判定
        if (currentTimeMillis > systemTimeMillis) {
            synchronizedNum = 0L;
            systemTimeMillis = currentTimeMillis;
        } else {
            synchronizedNum++;
        }
        key = sdf.format(new Date(systemTimeMillis));
        primaryUniqueKey.append(key).append(synchronizedNum);
        return primaryUniqueKey.toString();
    }

    public static void main(String[] args) {
        try {
            for (int i = 0; i < 100; i++) {
                String k = getPrimaryKey();
                System.out.println(k);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成业务单号
     * 前缀+13位时间戳+用户id
     */
    public String generateBusiSn(Integer tradeType, String customerId) {
        String prefix = "";
        switch (tradeType) {
            // rmb交易类型：0-提现，1-商品成交收益，2-商品服务费，3-商品推荐费，4-商品平台费(大b)，5-开通联盟商家平台费，
            // 6-开通联盟商家收益，7-开通加盟代理平台费，8-开通加盟代理收益，9-商品平台费(联汇)，10-商城商品收益，11-提现手续费
            case 1:
            case 10:
                prefix = "AB";
                break;
            case 2:
                prefix = "AE";
                break;
            case 3:
                prefix = "AF";
                break;
            case 4:
                prefix = "AC";
                break;
            case 5:
                prefix = "BD";
                break;
            case 6:
                prefix = "BB";
                break;
            case 7:
                prefix = "CD";
                break;
            case 8:
            case 13:
                prefix = "CB";
                break;
            case 9:
                prefix = "AD";
                break;
            case 12:
                prefix = "F";
                break;
            case 20://开通联盟商家平台抽成（大麦）
                prefix = "BC";
                break;
            case 21://开通加盟代理平台抽成（大麦）
                prefix = "CC";
                break;
            case 23://vip
                prefix = "V";
                break;
            case 31://31-体检费用
                prefix = "I";
                break;
            case 32://32-体检分佣
                prefix = "IB";
                break;
            case 33://33-体检充值
                prefix = "J";
            default:
                break;
        }
        return prefix + System.currentTimeMillis() + customerId;
    }

}
